from django.db import models
from .products import Products
from utils.modelMixin import statusFiledMixin, timeFiledMixin

# Create your models here.
STATUS = ((1, "上线"), (0, "下线"))


class ServerInfo(
    statusFiledMixin.StatusFiled, timeFiledMixin.CreateAndUpdateTimeFiled, models.Model
):
    name = models.CharField(max_length=50, verbose_name="服务名称", db_index=True)
    describe = models.CharField(max_length=128, verbose_name="描述")
    product = models.ForeignKey(
        Products, verbose_name="归属产品", on_delete=models.DO_NOTHING
    )

    class Meta:
        db_table = "se_server_info"
        verbose_name = "服务信息表"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return self.name


class HostInfo(models.Model):
    ENV_CHOICES = ((0, "release"), (1, "test1"), (2, "test3"))
    server = models.ForeignKey(
        "ServerInfo", verbose_name="主机名称", on_delete=models.CASCADE
    )
    server_ip = models.GenericIPAddressField(verbose_name="ip地址", blank=True, null=True)
    server_port = models.IntegerField(verbose_name="端口", blank=True, null=True)
    host = models.CharField(max_length=128, verbose_name="域名", blank=True, null=True)
    env = models.IntegerField(
        choices=ENV_CHOICES, verbose_name="测试环境", blank=True, null=True
    )

    class Meta:
        db_table = "se_host_info"
        verbose_name = "主机信息表"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return self.host


class Headers(models.Model):
    name = models.CharField(max_length=64, verbose_name="参数名称", db_index=True)
    key = models.CharField(max_length=32, verbose_name="键")
    values = models.CharField(max_length=255, verbose_name="值")

    class Meta:
        db_table = "se_api_headers"
        verbose_name = "头部信息表"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return f"{self.key}: {self.values}"


class Params(models.Model):
    TYPE_CHOICES = (
        (0, "str"),
        (1, "int"),
        (2, "float"),
        (3, "bool"),
        (4, "list"),
        (5, "dict"),
    )
    name = models.CharField(max_length=64, verbose_name="参数名称", db_index=True)
    key = models.CharField(max_length=32, verbose_name="键", unique=True)
    type = models.IntegerField(choices=TYPE_CHOICES, verbose_name="数据类型")
    is_necessary = models.BooleanField(verbose_name="是否必传", default=True)
    describe = models.CharField(max_length=128, verbose_name="描述", blank=True)
    http_api = models.ForeignKey(
        "HttpApi", verbose_name="关联接口", on_delete=models.CASCADE
    )

    class Meta:
        db_table = "se_api_params"
        verbose_name = "参数信息表"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return self.name


class HttpApi(statusFiledMixin.StatusFiled, models.Model):
    METHOD_CHOICES = (
        ("GET", "GET"),
        ("POST", "POST"),
        ("PUT", "PUT"),
        ("DELETE", "DELETE"),
        ("PATCH", "PATCH"),
        ("HEAD", "HEAD"),
        ("OPTIONS", "OPTIONS"),
        ("TRACE", "TRACE"),
    )
    server = models.ForeignKey(
        "ServerInfo", on_delete=models.CASCADE, verbose_name="所属服务"
    )
    name = models.CharField(max_length=50, verbose_name="API名称", db_index=True)
    index = models.CharField(
        max_length=32, unique=True, db_index=True, verbose_name="索引", help_text="接口关键字"
    )
    uri = models.CharField(max_length=255, verbose_name="API URI", db_index=True)
    method = models.CharField(
        max_length=10, choices=METHOD_CHOICES, verbose_name="HTTP方法", default="POST"
    )
    headers = models.ManyToManyField("Headers", verbose_name="请求头")

    class Meta:
        db_table = "se_http_api"
        verbose_name = "HTTP API表"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return f"{self.name}-{self.uri}"


class DubboApi(statusFiledMixin.StatusFiled, models.Model):
    server = models.ForeignKey(
        "ServerInfo", on_delete=models.CASCADE, verbose_name="所属服务"
    )
    name = models.CharField(max_length=50, verbose_name="API名称", db_index=True)
    class_name = models.CharField(max_length=255, verbose_name="类", default="Test")
    method = models.CharField(
        max_length=68, verbose_name="方法", db_index=True, default="test"
    )

    class Meta:
        db_table = "se_dubbo_api"
        verbose_name = "Dubbo API表"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return self.name
